<template>
	<view class="container">
		<view class="wrapper">
			<view class="main" >
				<scroll-view class="scroll" :scroll-y="true" :scroll-top="scrollTop" :show-scrollbar="false"
					:refresher-threshold="50" :refresher-triggered="refreshStatus" :lower-threshold="100"
					:scroll-with-animation="true" refresher-enabled="true" @refresherrefresh="onRefreshData"
					@scrolltolower="onLoadData" @scroll="onScroll">
					
					<view class="section" v-for="(item, index) in list" :key="index">
						<view class="card" style="align-items: center;justify-items: center;">
							<u-avatar size="200" shape="square" :src="item.img[0]"></u-avatar>
							<view class="content">
								<view class="pdleft">
									<view class="desc">
										场地名称：{{ item.locationName }}
									</view>
									<view  class="desc">
										场地类型：{{ venueTypeMap[item.locationType] }} {{ item.outside==1?'室内场':'室外场' }}
									</view>
									
									<view  class="desc">
										负责人：{{ item.manager }}
									</view>
									<view  class="desc">
										场地状态：{{ item.operateStatus==1?'营业中':'未营业' }}
									</view>
								</view>
							</view>
						</view>
						<view style="border-top: 1px solid #e5e5e5;padding: 20rpx 10rpx 0rpx 10rpx;">
							<u-grid :border="true" :col="4">
								<u-grid-item>
									<text style="font-size: 30rpx;color: #666666;" @click="delLoction(item.id)">删除</text>
								</u-grid-item>
								<u-grid-item >
									<text style="font-size: 30rpx;color: #666666;" @click="editLoction(item.id)">编辑</text>
								</u-grid-item>
								<u-grid-item >
									<text style="font-size: 30rpx;color: #666666;" @click="reservationCostLoction(item.id)">费用设置</text>
								</u-grid-item >
								<u-grid-item>
									<text style="font-size: 30rpx;color: #155ccf;" @click="statusLoction(item.id, item.operateStatus)">{{ item.operateStatus==1?'关闭营业':'开启营业' }}</text>
								</u-grid-item >
							</u-grid>
						</view>
					</view>
					
					<view class="nullData" v-if="page == 1 && list.length <=0">
						<view>
							<view>
								<image src="../../../static/image/notdata.png" class="nullImage"></image>
							</view>
							<view class="nullText">
								暂无记录
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
		<view style="height: 70px;background-color: #ffffff;position: fixed;bottom: 0px;width: 100%; ">
			<view style="padding:30rpx">
				<u-button text="新增场地" @click="addVenueLocation" color="#155CCF"></u-button>
			</view>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//当前页数
				page: 1,
				//每页显示条数
				size: 10,
				//列表数据
				list: [],
				//总页数
				pages: 0,
				//监听滚动
				scrollTop: 0,
				realScrollTop: 0,
				refreshStatus: false,
				//场地类型字典
				venueTypeMap: {}
			}
		},
		onLoad() {
			//获取场地类型
			this.venueTypeDict();
		},
		onShow() {
			this.fetchList();
		},
		methods: {
			// 获取场地记录
			fetchList() {
				uni.showLoading({
					title: "加载中",
					mask: true
				})
				var params = {};
				params.current = this.page;
				params.size = this.size;
				params.userId = uni.getStorageSync('userId');
				this.$request({
					url: '/blade-merchant-app/merchant-app/venue-location/location-page',
					data: params
				}).then(res => {
					const {
						records = [],
							pages
					} = res.data;
					
					records.forEach(item => {
						item.img = item.img.split(',');
					})
					
					if (this.page === 1) {
						this.list = records;
					} else {
						this.list = this.list.concat(records);
					}
					this.pages = pages || 0;
					setTimeout(() => {
						this.refreshStatus = false;
						uni.hideLoading();
					}, 300);
				})
			},
			// 监听滚动事件
			onScroll(e) {
				this.realScrollTop = e.detail.scrollTop;
			},
			// 刷新数据
			onRefreshData() {
				this.refreshStatus = true;
				this.page = 1;
				this.list = [];
				this.fetchList();
			},
			// 加载数据
			onLoadData() {
				if (this.page < this.pages) {
					this.page += 1;
					this.fetchList();
				}
			},
			//字典场地类型
			venueTypeDict(){
				this.$request({
					url: '/blade-client-business/app/dict/getDictList',
					type: 'GET',
					data: {'code': 'venue_type'}
				}).then(res => {
					var venueTypeMap = {};
					res.data.forEach(item => {
						venueTypeMap[item.dictKey] = item.dictValue;
					});
					this.venueTypeMap = venueTypeMap;
				})
			},
			//新增场地
			addVenueLocation(){
				var venueId = uni.getStorageSync('venueId');
				if(venueId && (venueId != '0') ){
					this.onLink('/pagesWorkbenches/pages/venueLocation/mainten/mainten')
				} else {
					uni.showModal({
					   title: '无法新增场地',
					   content: '您暂未申请场馆，请申请场馆且审核通过后重试',
					   showCancel: false,
					   success: function (res) {}
					});
				} 
			},
			//删除场地
			delLoction(id) {
				var that = this;
				uni.showModal({
				   title: '场地删除操作',
				   content: '是否确认删除此场地，删除后不可恢复？',
				   showCancel: true,
				   success: function (res) {
					   if (res.confirm) {
							uni.showLoading({
								title: '删除中',
								mask: true
							})
							that.$request({
								url: `/blade-merchant-app/merchant-app/venue-location/remove`,
								type: 'POST',
								data: {
									ids: id
								}
							}).then(res => {
								setTimeout(()=>{
									uni.hideLoading();
									that.fetchList();
								},800)
							}).catch(err =>{
								uni.hideLoading();
							})
					   }
				   }
				});
			},
			//编辑场地
			editLoction(id) {
				this.onLink('/pagesWorkbenches/pages/venueLocation/mainten/mainten?id='+id)
			},
			//场地费用设置
			reservationCostLoction(id) {
				this.onLink('/pagesWorkbenches/pages/venueLocation/cost/cost?id='+id)
			},
			//场地状态变更 operateStatus：1营业 其他未营业
			statusLoction(id, operateStatus) {
				uni.showLoading({
					title: '切换中',
					mask: true
				})
				var _url = '/blade-merchant-app/merchant-app/venue-location/enable?ids='+id;//启用
				if(operateStatus == 1){
					_url = '/blade-merchant-app/merchant-app/venue-location/disabled?ids='+id;//闭场
				}
				this.$request({
					url: _url,
					type: 'POST',
					data: {
						ids: id
					}
				}).then(res => {
					setTimeout(()=>{
						uni.hideLoading();
						if(operateStatus == 1){
							uni.$u.toast("场地成功关闭")
						} else {
							uni.$u.toast("场地成功开启运营")
						}
						this.fetchList();
					},800)
				}).catch(err =>{
					uni.hideLoading();
				})
			},
			onLink(path) {
				uni.navigateTo({
					url: path
				})
			},
		}
	}
</script>


<style lang="scss">
	page {
		height: 100%;
		overflow: hidden;
		background: #f1f1f1;

		.container {
			height: 100%;

			.wrapper {
				display: flex;
				flex-direction: column;
				height: calc(100% - 70px);

				.header {
					display: flex;
					flex-direction: row;
					padding: 52rpx 0;
					color: #fff;
					background-color: #22A5FF;
					z-index: 1;

					.subsection {
						background: #ffffff; padding: 10rpx 10rpx;
					}
				}

				.main {
					flex: 1;
					height: 100%;

					.scroll {
						height: 100%;

						.section {
							padding: 30rpx 30rpx 30rpx 30rpx;
							background-color: #ffffff;
							margin-bottom: 30rpx;

							.card {
								display: inline-flex;
								
								.imgs {
									width: 250rpx;height: 250rpx;
								}
								.content {
									width: 100%;
									
									.pdleft {
										padding-left:30rpx;
										.title {
											font-size: 35rpx;font-weight: 600;
										}
										.desc {
											font-size: 30rpx;color: #666666;line-height: 50rpx;
										}
										.price {
											color: red;
											.rmb {
												font-size: 30rpx;
											}
											.amount {
												font-size: 33rpx;
											}
										}
									}
									
									
								}
								
							}

							
						}
					}
				}
			}
		}
		
		.nullData {
			height: 100%;
			display: flex;
			-webkit-box-pack: center;
			justify-content: center;
			-webkit-box-align: center;
			align-items: center;
			
			.nullImage {
				width: 120px;height: 120px;
			}
			.nullText {
				color:#666666;font-size:29rpx;padding-top:15rpx;text-align: center;
			}
			
		}
	}
	
</style>
